/*
 Copyright (C) 2008  Maurizio Monticelli

 This program is free software; you can redistribute it and/or modify it under the terms of the 
 GNU General Public License as published by the Free Software Foundation; either version 2 of the 
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 Public License for more details.

 You should have received a copy of the GNU General Public License along with this program; if not, write 
 to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 
*/


package ch.justdoit.andriodrpncalc.model;

import java.util.List;

import ch.justdoit.andriodrpncalc.view.OnStackChangedListener;

public class Stack {
	
	private java.util.Stack<CalcNumber> mStack;
	private Model mModel;
	private OnStackChangedListener onStackChangedListener;
	
	public Stack(Model _model) {
		mStack = new java.util.Stack<CalcNumber>();
		mModel = _model;
	}
	
	public void refeshStack() {
		
		// TODO: better implemetation... Number send Changed-Event to View...
		if (onStackChangedListener != null) {
			onStackChangedListener.onChanged();
		}
	}
	
	public void setOnStackChangedListener(
			OnStackChangedListener _onStackChangedListener) {
		onStackChangedListener = _onStackChangedListener;
		
	}
	public int size() {
		return mStack.size();
	}
	
	public void Put(Double value) {
		CalcNumber number = new CalcNumber(value, mModel);
		this.Put(number);
	}


	public void Put(CalcNumber value) {
		mStack.push(value);
		if (onStackChangedListener != null) {
			onStackChangedListener.onChanged();
		}
	}
	
	public Double Peek() {
		if (!mStack.isEmpty()) {
			CalcNumber result = (CalcNumber) mStack.peek();
			return result.getDouble();
		}
		return null;
	}

	public CalcNumber PeekNumber() {
		if (!mStack.isEmpty()) {
			CalcNumber result = (CalcNumber) mStack.peek();
			return result;
		}
		return null;
	}

	public CalcNumber PeekNumber(int position) {
		if (position > -1 && position <  mStack.size()) {
			if (!mStack.isEmpty()) {
				CalcNumber result = (CalcNumber) mStack.get(position);
				return result;
			}
		}
		return null;
	}

	public Double Peek(int position) {
		if (position > -1 && position <  mStack.size()) {
			if (!mStack.isEmpty()) {
				CalcNumber result = (CalcNumber) mStack.get(position);
				return result.getDouble();
			}
		}
		return null;
	}
	
	
	public Double Pop () {
		if (!mStack.isEmpty()) {
			Double result = Peek();
			mStack.pop();
			if (onStackChangedListener != null) {
				onStackChangedListener.onChanged();
			}
			return result;
			
		}
		return null;
	}

	public CalcNumber PopNumber () {
		if (!mStack.isEmpty()) {
			CalcNumber result = (CalcNumber) mStack.pop();
			if (onStackChangedListener != null) {
				onStackChangedListener.onChanged();
			}
			return result;
			
		}
		return null;
	}

	public void Remove() {
		if (!mStack.isEmpty()) {
			this.mStack.pop();
			if (onStackChangedListener != null) {
				onStackChangedListener.onChanged();
			}
		}
	}
	
	public void Remove(int i) {
		if (mStack.size() > i) {
			mStack.remove(i);
			if (onStackChangedListener != null) {
				onStackChangedListener.onChanged();
			}
		}
	}
	
	public void Roll(int i) {
		if (mStack.size() > i && i != 0) {
			CalcNumber number = PeekNumber(i);
			mStack.remove(i);
			Put(number);
		}
	}
	
	public void RollD(int i) {
		if (mStack.size() > i && i != 0) {
			CalcNumber number = PeekNumber();
			mStack.pop();
			mStack.add(i, number);
			if (onStackChangedListener != null) {
				onStackChangedListener.onChanged();
			}
		}
	}
	public List<CalcNumber> getNumberList() {
		return mStack;
	}
	
	public List<String> getList() {
		List <String> result = new java.util.Stack<String>();
		for (int i = 0; i < mStack.size(); i++) {
			result.add( (mStack.size() - i) + ":\t " + mStack.get(i).toString() );
		}
		return result;
	}
	
	public List<CalcNumber> getList2() {
		return mStack;
	}

	public void Swap() {

		CalcNumber temp = PopNumber();
		CalcNumber tempb = PopNumber();
		Put(temp);
		Put(tempb);
	}

	public CalcNumber getItem(int i) {
		if (i > -1 && i <  mStack.size()) {
			if (!mStack.isEmpty()) {
				CalcNumber result = (CalcNumber) mStack.get(i);
				return result;
			}
		}
		return null;
	}
}
